package com.xo.xodemo.service.impl;

import com.xo.xodemo.mapper.db2.AdminUserMapper;
import com.xo.xodemo.model.LoginInfo_new;
import com.xo.xodemo.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private AdminUserMapper adminUserMapper;
    private final SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
    @Override
    public void renewaloneyear(String userId) {
        //如果已经到期在当前日期加一年 否则在到期时间加一年
        String validdate=adminUserMapper.selectValiddate(userId);
        //
        if(simpleDateFormat.format(new Date()).compareTo(validdate)>0){
            adminUserMapper.increaseYearToday(userId);
        }
        else {
            adminUserMapper.increaseYear(userId);
        }
    }

    @Override
    //以下数字用户等级代号
    //标准版购买
    public void bzbpurchase(String userId) throws ParseException {
        Integer etime=adminUserMapper.selectEtimeByuserlevel("4");
        //从当前日期加入标准期限生成到期时间
        adminUserMapper.updatenew(userId,"4",dateget(etime));
    }
    //经典版分期购买
    @Override
    public void jdbpartpurchase(String userId) throws ParseException {
        Integer etime=adminUserMapper.selectEtimeByuserlevel("5");
        //从当前日期加入标准期限生成到期时间
        adminUserMapper.updatenew(userId,"5",dateget(etime));
    }
    //经典版购买
    @Override
    public void jdbpurchase(String userId) throws ParseException {
        Integer etime=adminUserMapper.selectEtimeByuserlevel("2");
        //从当前日期加入标准期限生成到期时间
        adminUserMapper.updatenew(userId,"2",dateget(etime));
    }
    //尊享版购买
    @Override
    public void zxbpurchase(String userId) throws ParseException {
        Integer etime=adminUserMapper.selectEtimeByuserlevel("3");

        //从当前日期加入标准期限生成到期时间
        adminUserMapper.updatenew(userId,"3",dateget(etime));
    }
    private  String dateget(Integer day) throws ParseException {
        Date date=new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, day);
        return simpleDateFormat.format(cal.getTime());
    }
}
